var tblView = {};

function display_obj(obj){
	alert(YAHOO.lang.JSON.stringify(obj));
}

function render_tbl_rows(wall_id, nos){
	var tv = tblView['tbl_'+wall_id];

	var num = tv.num;
	var cols = tv.cols;
	while(cols < num)
		cols += tv.cols;

	var rem = cols - num;
	while(rem < nos){
		add_rows(null, wall_id, tv.cols, cols);
		cols += tv.cols;
		rem = cols - num;
	}
}

function add_rows(tbl, wall_id, cols,  startindex){
	var tr = document.createElement('tr');

	for(var i =0; i < cols; i++){
		var td = document.createElement('td');
		td.id = 'ntd_'+wall_id+'_'+(startindex+i);
		tr.appendChild(td);
	}
	if(tbl && tbl.id){
		tbl.appendChild(tr);
	}else {
		tbl = document.getElementById('tbl_'+wall_id);
		tbl.appendChild(tr);
	}
}

function add_notepad_to_tbl(notepad, wall_id){
	render_tbl_rows(wall_id, 1);
	var tv = tblView['tbl_'+wall_id];
	var nid = 'ntd_'+wall_id +'_' + (tv.num++);
	var ntd = document.getElementById(nid);
	ntd.appendChild(notepad);
}

function append_wall_notes(wall_notes){
	var wall_id = wall_notes.wall_id;

	for ( var i in wall_notes.notes){
		var notepad = wall_notes.notes[i];
		var npe = get_notepad_element(notepad);
		add_notepad_to_tbl(npe,wall_id);
	}
}

function get_notepad_element(notepad){
	var ul = document.createElement('ul');
	ul.id = notepad.notepad_id;
	for(var i in notepad.notepad){
		var li = get_note_element(notepad.notepad[i]);
		ul.appendChild( li);
	}

	var ndiv = document.createElement('div');
	ndiv.appendChild(ul);
	ndiv.title = notepad.tag_list;
	ndiv.style.width = '200px';
        ndiv.style.backgroundColor = 'yellow';

	var dd = new YAHOO.util.DD(ndiv);
	dd.on('endDragEvent', function(e){
		//alert(ndiv.style.top+','+ndiv.style.left);
	}, dd, true);
	return ndiv;
}

function get_note_element(note){
	var li = document.createElement('li');
	li.id = note[0];
	li.innerHTML = note[1];
	return li;
}

function append_notes(notes){
	for( var i in notes){
		var note = notes[i];
		var ntd = document.createElement('td');
		ntd.innerHTML = note.note_txt;
		var ttd = document.createElement('td');
		ttd.innerHTML = '&nbsp;'+note.note_tags;

		var tr = document.createElement('tr');
		tr.id = note.note_id;
		tr.appendChild(ntd);
		tr.appendChild(ttd);
		var tbl = document.getElementById('notes_tbl').childNodes[1];
		tbl.appendChild(tr);
	}
}

function process_response(responseText){
	var res = YAHOO.lang.JSON.parse(responseText);
	if(res.status == 'success' ){
		if (res.notes)
			append_notes(res.notes);
		if(res.wall_notes){
			append_wall_notes(res.wall_notes);
		}
		if(res.scratchpad){
			append_sp_notes(res.scratchpad);
		}
	}
}

function add_notes(){
        var notes = document.getElementById('note_txt').value;
        var tags = document.getElementById('note_tags').value;
        var params = "operation=add_note&note_txt="+notes+"&note_tags="+tags;

	post(params);
}

function append_sp_notes(scratchpad){
	var spn = document.createElement('ul');
	var spd = document.getElementById('scratchpad_div');
	spd.innerHTML = "";
	for( i in scratchpad){
		var li = get_note_element(scratchpad[i]);
		spn.appendChild(li);
	}
	spd.appendChild(spn);
}

function get_all_notes(){
	var params = "operation=get_all_notes";

	post(params);
}


function fetch_wall_notes(wall_id){
	var params = "operation=get_wall_notes&wall_id="+wall_id;
	post(params);
}

function post(params){
	var callBack = {
		success:function(o){
			process_response(o.responseText);
		},
		failure:function(o){
			alert('AJAX error');
		},
		argument:null
	};
        var url = "ajax/notes_operation.php";
	var request = YAHOO.util.Connect.asyncRequest('POST', url, callBack, params); 
}


function render_wall(wall_id){
	var wdiv = document.getElementById('wall_'+wall_id);

	if(!document.getElementById('idiv_'+wall_id)){
		fetch_wall_notes(wall_id);
		wdiv.innerHTML = '';
		var idiv = document.createElement('div');
		idiv.id = 'idiv_'+wall_id;

		var tbl = document.createElement('table');
		tbl.id = 'tbl_'+wall_id;
		tblView[tbl.id] = {};
		tblView[tbl.id].num = 0;
		tblView[tbl.id].cols = parseInt(wdiv.clientWidth/200);
		add_rows(tbl,wall_id,tblView[tbl.id].cols, 0);

		var tinput = document.createElement('input');
		tinput.id = 't_'+wall_id;
		tinput.name = 'tinput';
		var acdiv = document.createElement('div');

		var tsubmit = document.createElement('input');
		tsubmit.id = 's_'+wall_id;
		tsubmit.name = 'tsubmit';
		tsubmit.value = 'Search Tags';
		tsubmit.type = 'button';
		tsubmit.addEventListener('click', function (){
			var taglist = tinput.value;
			var params = 'operation=add_notepad&wall_id='+wall_id+'&taglist='+taglist;
			post(params);
		}, true);
		idiv.appendChild(tbl);
		idiv.appendChild(tinput);
		idiv.appendChild(document.createElement('br'));
		idiv.appendChild(tsubmit);
		idiv.appendChild(document.createElement('br'));
		idiv.appendChild(acdiv);

		wdiv.appendChild(idiv);
		var oDS = new YAHOO.util.XHRDataSource("tags.php");
		oDS.responseType = YAHOO.util.XHRDataSource.TYPE_TEXT;
		// Define the schema of the delimited results
		oDS.responseSchema = {
        		recordDelim: "\n",
        		fieldDelim: "\t"
    		};
    		// Enable caching
    		oDS.maxCacheEntries = 5;

    		// Instantiate the AutoComplete
    		var oAC = new YAHOO.widget.AutoComplete(tinput, acdiv, oDS);
	}

}

function get_scratchpad_notes(){
	var tgbox = document.getElementById("s_note_tags");
	var params = "operation=get_sp_notes&taglist="+tgbox.value;
	post(params);
} 
